﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ServicesSheet_QueryUseCar : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            QDate.Text = DateTime.Now.ToShortDateString();
            NewDataSet();

            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "~/Help/ServicesSheet/查询公司车辆登记使用.htm";
            master.PageName = "查询公司车辆登记使用";
            master.PicUrl = "";
            master.IntiBar();

            // InitializeComponent();

            // Master.DataSource = WMaster;

        }

    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServicesSheet_QueryUseCar));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = false;
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "ServiceSheet.View_Services_UseCars";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        NewDataSet();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        NewDataSet();

    }

    private void NewDataSet()
    {
        DataSet Tmp;
        string Sql = "";

        InitializeComponent();

        Sql = Sql + "   SELECT Services_SV_Cars.CarName, Services_SV_Cars.CarId, ";
        Sql = Sql + "  Services_UseCars.EmployeeId, Services_UseCars.UCDate, ";
        Sql = Sql + "  Services_UseCars.UOutHour, Services_UseCars.UOutMinute, ";
        Sql = Sql + "   Services_UseCars.UBackHour, Services_UseCars.UBackMinute, ";
        Sql = Sql + "    Employees.EmpName, ";
        Sql = Sql + "   Department.DepartmentName, Services_UseCars.FlowFlag, ";
        Sql = Sql + "     Services_UseCars.PeoCount, Services_UseCars.PreWay";
        Sql = Sql + "   FROM Employees INNER JOIN";
        Sql = Sql + "    Services_UseCars ON ";
        Sql = Sql + "   Employees.EmployeeID = Services_UseCars.EmployeeId INNER JOIN";
        Sql = Sql + "  Department ON ";
        Sql = Sql + "  Employees.DepartmentId = Department.DepartmentId RIGHT OUTER JOIN ";
        Sql = Sql + "   Services_SV_Cars ON Services_UseCars.CarId = Services_SV_Cars.CarId";

        Sql = Sql + " Where (1=1) ";
        if (QDate.Text != "0" && QDate.Text.Length > 0) Sql = Sql + string.Format(" And(Services_UseCars.UCDate = '{0}') ", QDate.Text);
        if (WebDropDownList1.SelectedValue != "0" && WebDropDownList1.SelectedValue.Length > 0) Sql = Sql + " and (Services_SV_Cars.CarId=" + WebDropDownList1.SelectedValue + ") ";


        Tmp = WMaster.Execute(Sql);

        DataTable svTable;
        svTable = MakeNewWTable();
        DataRow SR;

        for (int i = 0; i < Tmp.Tables[0].Rows.Count; i++)
        {
            SR = svTable.NewRow();

            SR["CarId"] = Tmp.Tables[0].Rows[i]["CarId"];
            SR["EmployeeId"] = Tmp.Tables[0].Rows[i]["EmployeeId"];
            SR["CarName"] = Tmp.Tables[0].Rows[i]["CarName"];
            SR["EmpName"] = Tmp.Tables[0].Rows[i]["EmpName"];
            SR["DepartmentName"] = Tmp.Tables[0].Rows[i]["DepartmentName"];
            SR["UCDate"] = Tmp.Tables[0].Rows[i]["UCDate"];
            SR["UCTime"] = GetDateStr(Tmp.Tables[0].Rows[i]["UOutHour"], Tmp.Tables[0].Rows[i]["UOutMinute"], Tmp.Tables[0].Rows[i]["UBackHour"], Tmp.Tables[0].Rows[i]["UBackMinute"]);

            SR["PreWay"] = Tmp.Tables[0].Rows[i]["PreWay"];
            string fl = (Tmp.Tables[0].Rows[i]["PreWay"] == Convert.DBNull) ? "" : (string)Tmp.Tables[0].Rows[i]["FlowFlag"];

            fl = (fl == "Z") ? "通过申请" : "在审核";
            fl = (Tmp.Tables[0].Rows[i]["EmployeeId"] == Convert.DBNull) ? "" : fl;
            SR["FlowFlag"] = fl;
            svTable.Rows.Add(SR);
        }

        wgvMaster.DataSource = svTable;
        wgvMaster.DataBind();

    }

    public String GetDateStr(object UOutHour, object UOutMinute, object UBackHour, object UBackMinute)
    {
        string Uh = (UOutHour == Convert.DBNull) ? "" : (string)UOutHour;
        string Um = (UOutMinute == Convert.DBNull) ? "" : (string)UOutMinute;
        string UBh = (UBackHour == Convert.DBNull) ? "" : (string)UBackHour;
        string UBm = (UBackMinute == Convert.DBNull) ? "" : (string)UBackMinute;
        return (UOutHour == Convert.DBNull) ? "" : (Uh + ":" + Um + "~" + UBh + ":" + UBm);
    }

    private DataTable MakeNewWTable()
    {
        DataTable WTable = new DataTable("WMaster");

        {
            WTable.Columns.Add(new DataColumn("CarId", typeof(Int32)));
            WTable.Columns.Add(new DataColumn("EmployeeId", typeof(Int32)));
            WTable.Columns.Add(new DataColumn("CarName", typeof(string)));
            WTable.Columns.Add(new DataColumn("EmpName", typeof(string)));
            WTable.Columns.Add(new DataColumn("DepartmentName", typeof(string)));
            WTable.Columns.Add(new DataColumn("UCDate", typeof(DateTime)));
            WTable.Columns.Add(new DataColumn("UCTime", typeof(string)));
            WTable.Columns.Add(new DataColumn("PreWay", typeof(string)));
            WTable.Columns.Add(new DataColumn("PeoCount", typeof(Int32)));

            WTable.Columns.Add(new DataColumn("FlowFlag", typeof(string)));

        }
        return WTable;

    }


    protected void wgvMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        wgvMaster.PageIndex = e.NewPageIndex;
        NewDataSet();
    }
}
